﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.WinConsole;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MusicPhotograph.VideoHelper
{
    internal class FfmpegHelper
    {
        /// <summary>
        /// 引用环境位置
        /// </summary>
        public readonly static string FFMpegPath = StaticValue.BinImportPath + "ffmpeg.exe";

        /// <summary>
        /// 创建MP4
        /// </summary>
        public readonly static string ImgToMp4Temp = StaticValue.TempPath + "movie.mp4";

        /// <summary>
        /// 淡入淡出Img处理
        /// </summary>
        public readonly static string FadeMp4Temp = StaticValue.TempPath + "fade.mp4";

        /// <summary>
        /// 图片转文字
        /// </summary>
        public static readonly string CreateMP4Commad = " -y -loop 1 -r 30 -i \"{0}\" -t 00:00:03 -s 1280:720 -b:v 2048k \"{1}\"";

        /// <summary>
        /// 淡入淡出
        /// </summary>
        public readonly static string FadeCommad = " -y -i \"{0}\"   -filter_complex \"fade=in:st=0:d=0.5:color=DarkGray,fade=out:st=2.5:d=0.5:color=DarkGray\"   \"{1}\"";

        /// <summary>
        /// 合并视频文件
        /// </summary>
        public readonly static string MergeCommad = "  -y -safe 0 -f concat -i \"{0}\" -c copy \"{1}\"";

        /// <summary>
        /// 添加音频文件
        /// </summary>
        public readonly static string AddAudioCommad = " -y -i \"{0}\" -f lavfi -i amovie=\"{1}\":loop=10,asetpts=N/SR/TB -shortest \"{2}\"";

        /// <summary>
        /// 添加音频文件的方法
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="AudioFilePath"></param>
        /// <param name="OutFilePath"></param>
        /// <returns></returns>
        public string AddAudio(string FilePath, string AudioFilePath, string OutFilePath)
        {
            AudioFilePath = AudioFilePath.Replace("\\", "/");
            AudioFilePath = AudioFilePath.Replace(":", "\\\\:");
            Console.WriteLine(new ProcessHelper().RunCmdReadError(FFMpegPath, string.Format(AddAudioCommad, FilePath, AudioFilePath, OutFilePath)));
            PrintLog.I(string.Format(AddAudioCommad, FilePath, AudioFilePath, OutFilePath));
            return OutFilePath;
        }

        /// <summary>
        /// 合并视频
        /// </summary>
        /// <param name="FileListPath"></param>
        /// <param name="MergeFilePath"></param>
        /// <returns></returns>
        public string MergeMoive(string FileListPath, string MergeFilePath)
        {
            new ProcessHelper().RunCmdReadError(FFMpegPath, string.Format(MergeCommad, FileListPath, MergeFilePath));
            Console.WriteLine(FFMpegPath, string.Format(MergeCommad, FileListPath, MergeFilePath));
            return MergeFilePath;
        }

        /// <summary>
        /// 创建淡入淡出视频
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public string CreateFadeMoive(string FileName)
        {
            new ProcessHelper().RunCmdReadError(FFMpegPath, string.Format(FadeCommad, FileName, FadeMp4Temp));
            return FadeMp4Temp;
        }

        /// <summary>
        /// 异步打印日志，当前已经没什么函数调用他了
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void receivedEventHandler(object sender, DataReceivedEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Data))

            {
                Console.WriteLine(e.Data);
            }
        }

        /// <summary>
        /// 创建图片视频
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public string CreateImageMoive(string FileName)
        {
            new ProcessHelper().RunCmdReadError(FFMpegPath, string.Format(CreateMP4Commad, FileName, ImgToMp4Temp));
            return ImgToMp4Temp;
        }
    }
}